/*
  Copyright 2006 by Sean Luke and George Mason University
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
 */

package bojan.robots.simulation;

import java.awt.Color;

import javax.swing.JFrame;

import sim.display.Controller;
import sim.display.Display2D;
import sim.display.GUIState;
import sim.engine.SimState;
import sim.portrayal.continuous.ContinuousPortrayal2D;
import src.bojan.Navigator;
import src.bojan.Unknown;

public class SimulationWithUI extends GUIState {
	public Display2D display;
	public JFrame displayFrame;

	private ContinuousPortrayal2D yardPortrayal = new ContinuousPortrayal2D();

	// uncomment this to try out trails (also need to uncomment out some others
	// in this file, look around)
	// you'll also need to cause a Bug to wander a lot more in order to see the
	// trail -- I suggest setting
	// its idealTemperature to 0 in the Inspector.
	/*
	 * SparseGridPortrayal2D trailsPortrayal = new SparseGridPortrayal2D();
	 */

	public static void main(String[] args) {
		new SimulationWithUI().createController();
	}

	public SimulationWithUI() {
		super(new Simulation(System.currentTimeMillis()));
	}

	public SimulationWithUI(SimState state) {
		super(state);
	}

	public static String getName() {
		return "";
	}

	public Object getSimulationInspectedObject() {
		return state;
	} // non-volatile

	public void start() {
		super.start();
		// set up our portrayals
		setupPortrayals();
	}

	public void load(SimState state) {
		super.load(state);
		// we now have new grids. Set up the portrayals to reflect that
		setupPortrayals();
	}

	// This is called by start() and by load() because they both had this code
	// so I didn't have to type it twice :-)
	public void setupPortrayals() {
		Simulation students = (Simulation) state;
		// tell the portrayals what to portray and how to portray them
		yardPortrayal.setField(students.grid);
		// yardPortrayal.setPortrayalForClass(Navigator.class, new
		// OvalPortrayal2D());
		yardPortrayal.setPortrayalForClass(Navigator.class,
				new sim.portrayal.simple.OvalPortrayal2D(Color.white));
		yardPortrayal.setPortrayalForClass(Unknown.class,
				new sim.portrayal.simple.RectanglePortrayal2D(Color.black));
		yardPortrayal.setPortrayalForClass(Obstacle.class,
				new sim.portrayal.simple.RectanglePortrayal2D(Color.BLUE));
		// reschedule the displayer
		display.reset();
		display.setBackdrop(Color.black);
		// redraw the display
		display.repaint();
	}

	public void init(final Controller c) {
		super.init(c);
		display = new Display2D(300, 300, this, 1);
		display.setClipping(false);
		displayFrame = display.createFrame();
		displayFrame.setTitle("Multirobot coordination");
		c.registerFrame(displayFrame); // so the frame appears in the "Display"
										// list
		displayFrame.setVisible(true);
		display.attach(yardPortrayal, "ExploredSurface");
	}

	public void quit() {
		super.quit();

		if (displayFrame != null)
			displayFrame.dispose();
		displayFrame = null; // let gc
		display = null; // let gc
	}
}
